<span id="${uuid}"></span>
<%--课题组人员管理--%>
<div class="content margin5" style="overflow: hidden;zoom:1;">
    <%--<h4 class="ml4 mb4n">课题组组长</h4>--%>
    <%--<ul data-id="leaders" class="mt5 user-qes-selected-box">--%>

    <%--</ul>--%>
    <%--<div class="mt10" style="height: 5px"></div>--%>
    <%--<h4 class="ml4 mb4n">课题组用户列表</h4>--%>

    <div class="pageGrid" <%--style="*width: 98%;*height:400px"--%>>
        <table data-id="dg_org"></table>
        <div data-id="nsdb" style="padding: 5px; height: auto">
            <shiro:hasPermission name="organization:user:add">
                <a data-href="${pageContext.request.contextPath}/app/api/organization/toAddOrgUser" data-key="add"
                   iconCls="fa fa-plus fa-lg" plain="true">新增</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="organization:user:delete">
                <a data-href="${pageContext.request.contextPath}/app/api/organization/deleteOrgUsers" data-key="del"
                   iconCls="fa fa-trash fa-lg" plain="true">删除</a>
            </shiro:hasPermission>
            <a data-href="${pageContext.request.contextPath}/app/api/organization/toSearchOrgUser" data-key="search"
               iconCls="fa fa-search fa-lg" plain="true">搜索</a>
            <a data-href="#" data-key="search_all" iconCls="fa fa-refresh fa-lg" plain="true">搜索全部</a>
        </div>
    </div>
    <%--<div data-id="org_tempate">--%>
        <%--<shiro:hasPermission name="organization:leader:add">--%>
            <%--<a data-href="${pageContext.request.contextPath}/app/api/organization/addLeader" data-id="{{id}}"--%>
               <%--data-key="addLeader" iconCls="fa fa-user fa-lg" plain="true">设为组长</a>--%>
        <%--</shiro:hasPermission>--%>
        <%--<shiro:hasPermission name="organization:leader:delete">--%>
            <%--<a data-href="${pageContext.request.contextPath}/app/api/organization/delLeader" data-id="{{id}}"--%>
               <%--data-key="delLeader" iconCls="fa fa-user-times fa-lg" plain="true">取消设为组长</a>--%>
        <%--</shiro:hasPermission>--%>
    <%--</div>--%>
</div>
<script language="javascript" type="text/javascript">
    seajs.use(["apps/org/page.css"], function () {
        var uuid = "${uuid}";
        var page = new $.PageDomFactory(uuid);
        var pageListComponent = new $.PageListComponent(uuid, {
            view: {
                grid: "dg_org",
                myToolbar: "nsdb"
//                colbar: "org_tempate"
            },
            configs: {
                gridTitle: "课题组用户列表",
                gridUrl: '${pageContext.request.contextPath}/app/api/organization/findOrgUser?orgId=${organization.id}',
                gridColumns: [
                    {field: 'id', checkbox: true},
                    {field: 'sysUserMember.userNumber', title: '用户名', width: 50,align: 'center'},
                    {field: 'sysUserMember.userName', title: '姓名', width: 50,align: 'center'},
//                    {field: 'sysOrganization.orgName', title: '课题组名称', width: 150},
//                    // {field: 'sysUserMember.alias', title: '职务', width: 100},
//                    {field: 'sysUserMember.available', title: '状态', width: 100,
//                        formatter: function (value, rec) {
//                            return value == "1" ? "启用" : "停用";
//                        }
//                    },
//                    {field: 'role', title: '是否为组长', width: 100,
//                        formatter: function (value, rec) {
//                            return value == 1 ? "是" : "否";
//                        }
//                    }
                    {
                        field: 'sysUserMember.userHead',
                        title: '头像',
                        width: 50,
                        align: 'center',
                        formatter: function (value, row, index) {
                            var v = "${pageContext.request.contextPath}/print/upload/default/pic1.png";
                            if(value){
                                v = "${pageContext.request.contextPath}/print/" + value;
                            }
                            return '<img data-id="userHead" src="'+ v + '" style="width: 36px;height: 36px;" >';
                        }
                    },
                    {
                        field: 'sysUserMember.orgsDescription',
                        title: '小组',
                        width: 50,
                        align: 'center',
                        formatter: function (value, row, index) {
                            return value || "<a>N/A</a>";
                        }
                    },
                    {
                        field: 'sysUserMember.email',
                        title: '邮箱',
                        width: 50,
                        align: 'center',
                        formatter: function (value, row, index) {
                            return !value ? "<a>N/A</a>" : value;
                        }
                    },
                    {
                        field: 'sysUserMember.mobile',
                        title: '手机',
                        width: 50,
                        align: 'center',
                        formatter: function (value, row, index) {
                            return !value ? "<a>N/A</a>" : value;
                        }
                    },
                    {
                        field: 'sysUserMember.available',
                        title: '状态',
                        width: 50,
                        align: 'center',
                        formatter: function (value, row, index) {
                            if (value == "1"
                                    && value != null) {
                                return "启用";
                            } else {
                                return "禁用";
                            }
                        }
                    }
                ]
            },
            events: {
                renderColBarBefore: function (key, row) {
                    if (key == "addLeader") {
                        return row.role == 0;
                    }
                    if (key == "delLeader") {
                        return row.role == 1;
                    }
                },
                onClickColBtn: function (context, btn, row) {
                    switch (btn.key) {
                        case "addLeader":
                            whenLeader(context, btn.href, row.id);
                            break;
                        case "delLeader":
                            whenLeader(context, btn.href, row.id);
                            break;
                        default :
                            break;
                    }
                },
                onClickToolbarBtn: function (context, btn) {
                    switch (btn.key) {
                        case "search_all":
                            context.searchAll();
                            break;
                        case "search":
                            context.search(btn.href + "?orgId=${organization.id}", "小组成员搜索", "org_user_search");
                            break;
                        case "add":
                            whenAdd(context, btn.href);
                            break;
                        case "del":
                            whenDel(context, btn.href);
                            break;
                        default :
                            break;
                    }
                }
            }
        });
        var contextWindow = new $.PageWindow();

        function whenAdd(context, href) {
            contextWindow.create({
                href: href + "?orgId=${organization.id}",
                method: "organization_add_user_save",
                title: "添加小组成员",
                callback: function (status) {
                    if (status) {
                        context.grid.datagrid("reload");
                    }
                }
            });
            contextWindow.open();
        }


        function whenDel(context, href) {
            var rows = context.grid.datagrid('getChecked');
            if (rows.length == 0) {
                $.messager.alert('信息', '请先选择一行', 'info');
                context.grid.datagrid('clearSelections');
                return;
            }
            if (rows.length > 0) {
                $.messager.confirm('确定', '确定要删除吗?', function (result) {
                    if (result) {
                        var ids = [];
                        for (var i = 0; i < rows.length; i++) {
                            ids.push(rows[i].id);
                        }
                        $.post(
                                href,
                                $.utils.buildJson({id: "${organization.id}", ids: ids}),
                                function (json) {
                                    if ($.isErrorRequest(json)) {
                                        $.messager.alert('信息', json.msg, 'info');
                                    } else {
                                        $.messager.alert('信息', '删除成功', 'info');
                                    }
                                    showLeader();
                                }
                        );
                    }
                });
            }
        };

        function whenLeader(context, href, id) {
            $.post(
                    href,
                    $.utils.buildJson({id: id}),
                    function () {
                        showLeader();
                    }
            );
        }

        function showLeader() {
            $.post(
                    "${pageContext.request.contextPath}/app/api/organization/findLeader",
                    $.utils.buildJson({orgId: "${organization.id}", role: 1}),
                    function (json) {
                        page.getJqDom("leaders").empty();
                        if (json.length == 0) {
                            page.getJqDom("leaders").html("<h4 style='color: #ccc;text-align: center;line-height: 32px'>暂未设置组长</h4>");
                        }
                        for (var i = 0; i < json.length; i++) {
                            var orgUser = json[i];
                            var htm = '<li data-id=' + orgUser.id + '>' +
                                    '<span title="' + orgUser.sysUserMember.userName + '">' + orgUser.sysUserMember.userName + '</span>' +
                                    '<span>(' + orgUser.sysUserMember.userNumber + ')</span>' +
                                    '<a href="javascript:void(0)" data-options="iconCls:\'fa fa-trash fa-lg\',plain:true"></a></li>';
                            $(htm).appendTo(page.getJqDom("leaders")).find("a").linkbutton({
                                onClick: function () {
                                    var id = $(this).parent().attr("data-id");
                                    $.post(
                                            "${pageContext.request.contextPath}/app/api/organization/delLeader",
                                            $.utils.buildJson({id: id}),
                                            function () {
                                                showLeader();
                                            }
                                    );
                                }
                            });
                        }
                    },
                    "json"
            );
            page.getJqDom("dg_org").datagrid("reload");
        }

        showLeader();
    });
</script>